package com.zone.core.base

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.zone.core.CoreApplication
import com.zone.core.helper.PermissionHelper
import pub.devrel.easypermissions.EasyPermissions

/**
 *Author: last
 *Time: 2024-1-21
 *Description: xxx
 */
open class BaseActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks,
    EasyPermissions.RationaleCallbacks {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handleStatusBar()
    }

    fun requestPermissions(
        permissions: Array<out String>,
        onGranted: () -> Unit,
        onDenied: (() -> Unit)? = null
    ) {
        PermissionHelper.requestPermissions(this, permissions, onGranted,onDenied)
    }


    /**
     * 状态栏是否是亮色
     * @return
     */
    open fun isLightStatusBar() = true

    /**
     * 状态栏是否透明
     */
    open fun isTransparentStatusBar() = true

    /**
     * 是否全屏
     */
    open fun isFullscreen() = true

    /**
     * 状态栏处理
     */
    private fun handleStatusBar() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = Color.TRANSPARENT
        }
        // 如果亮色，设置状态栏文字为黑色
        var systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

        if (isFullscreen()) {
            systemUiVisibility = systemUiVisibility or SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        window.decorView.systemUiVisibility = systemUiVisibility
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }


    /**
     * 权限申请失败
     */
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        PermissionHelper.onPermissionsDenied(this, requestCode, perms)
    }


    /**
     * 权限申请成功
     */
    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    /**
     * 权限申请提示弹出框点击拒绝
     */
    override fun onRationaleDenied(requestCode: Int) {
    }

    /**
     * 权限申请提示框点击确定
     */
    override fun onRationaleAccepted(requestCode: Int) {

    }

    override fun onDestroy() {
        super.onDestroy()
    }
}